package cn.tedu.ivos.dict.service.impl;

import cn.tedu.ivos.dict.mapper.DictMapper;
import cn.tedu.ivos.dict.pojo.dto.DictParam;
import cn.tedu.ivos.dict.pojo.dto.DictQuery;
import cn.tedu.ivos.dict.pojo.entity.Dict;
import cn.tedu.ivos.dict.pojo.vo.DictVO;
import cn.tedu.ivos.dict.service.DictService;
import cn.tedu.ivos.dictoption.mapper.DictOptionMapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;


@Slf4j
@Service
public class DictServiceImpl  implements DictService {


    @Autowired
    DictMapper dictMapper;
    @Autowired
    DictOptionMapper dictOptionMapper;
    @Override
    public List<DictVO> dictselect(DictQuery dictQuery) {
        log.debug("id:{}",dictQuery);
     List<DictVO>  list= dictMapper.selectdict(dictQuery);
        return list;
    }
    @Override
    public void dictsave(DictParam dictParam) {
        log.debug("dictParam:{}",dictParam);
        Dict dict=new Dict();
        BeanUtils.copyProperties(dictParam,dict);
        if(dict.getId()==null){
            dict.setCreateTime(new Date());
            dictMapper.insertdict(dict);
        }else{
            dict.setUpdateTime(new Date());
            dictMapper.updatedict(dict);
        }


    }

    @Override
    public void dictdelete(Long id) {
        log.debug("id:{}",id);
        dictMapper.deletedict(id);
        //删除字典 也需要把字典下所有的关联字典项删除!
        //此处的id就是当前被删除的字典的id
        dictOptionMapper.deleteByDictId(id);


    }


}
